module rotary_controller(
  input wire CCLK,
  input wire ROTA,
  input wire ROTB,
  input wire ROTCTR,
  output wire [7:0] LED
);

  reg [9:0] quad_count;
  assign LED[7:0] = quad_count[9:2];
  
  reg [1:0] current_state, next_state;
  reg       increment, decrement;
  
always @ (*) begin
  //Defaults
  increment = 1'b0;
  decrement = 1'b0;
  next_state = current_state;
  case(current_state)
    2'b11: begin
      if(~ROTA) begin
        next_state = 2'b01;
        increment = 1'b1;
      end else if(~ROTB) begin
        next_state = 2'b10;
        decrement = 1'b1;
      end
    end
    
    2'b01: begin
      if(~ROTB) begin
        next_state = 2'b00;
        increment = 1'b1;
      end else if (ROTA) begin
        next_state = 2'b11;
        decrement = 1'b1;
      end
    end
    
    2'b00: begin
      if(ROTA) begin
        next_state = 2'b10;
        increment = 1'b1;
      end else if (ROTB) begin
        next_state = 2'b01;
        decrement = 1'b1;
      end
    end
    
    2'b10: begin
      if(ROTB) begin
        next_state = 2'b11;
        increment = 1'b1;
      end else if (~ROTA) begin
        next_state = 2'b00;
        decrement = 1'b1;
      end
    end
  endcase
end

always @(posedge CCLK or posedge ROTCTR) begin
  if(ROTCTR) current_state <= 2'b11;
  else current_state <= next_state;
end

always @(posedge CCLK or posedge ROTCTR) begin
  if(ROTCTR) quad_count <= 1'b0;
  else if(increment) quad_count <= quad_count +1'b1;
  else if(decrement) quad_count <= quad_count -1'b1;
end

endmodule